package com.piepesken.mediscale.ui.overview

import android.annotation.SuppressLint
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.piepesken.mediscale.ui.components.StatisticCard
import com.piepesken.mediscale.ui.components.overview.Statistics
import java.text.SimpleDateFormat
import java.util.*
import com.piepesken.mediscale.R

@Composable
fun getTimeString(): String {
    val timeStringId = when (Calendar.getInstance().get(Calendar.HOUR_OF_DAY)) {
        in 8..11 -> R.string.morning
        in 12..18 -> R.string.afternoon
        in 18..20 -> R.string.evening
        else -> R.string.night
    }
    return stringResource(id = timeStringId)
}

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun OverviewScreen(
    navController: NavController
) {
    Scaffold(
        floatingActionButton = {
            FloatingActionButton(onClick = { navController.navigate("selectScale") }) {
                Icon(imageVector = Icons.Filled.Add, contentDescription = stringResource(id = R.string.addNewEntry))
            }
        }
    ) {
        Column {
            LargeTopAppBar(
                title = {
                    Text(
                        text = String.format(stringResource(id = R.string.greetings), getTimeString()),
                        fontSize = MaterialTheme.typography.displaySmall.fontSize,
                        fontWeight = MaterialTheme.typography.displaySmall.fontWeight,
                        fontStyle = MaterialTheme.typography.displaySmall.fontStyle
                    )
                },
            )

            Text(
                fontSize = MaterialTheme.typography.headlineSmall.fontSize,
                fontWeight = MaterialTheme.typography.headlineSmall.fontWeight,
                fontStyle = MaterialTheme.typography.headlineSmall.fontStyle,
                color = Color.Gray,
                modifier = Modifier.padding(start = 16.dp, top = 16.dp),
                text = SimpleDateFormat(stringResource(id = R.string.dateTemplate), Locale.getDefault()).format(Date())
            )

            LazyVerticalGrid(
                columns = GridCells.Adaptive(minSize = 160.dp),
                contentPadding = PaddingValues(16.dp)
            ) {
                items(Statistics.values().toList()) {
                        statistic -> StatisticCard(
                            title = stringResource(id = statistic.title),
                            value = statistic.value,
                            unit = statistic.unit,
                            textColor = if (isSystemInDarkTheme()) statistic.darkTextColor else statistic.textColor,
                            backgroundColor  = if (isSystemInDarkTheme()) statistic.darkBackgroundColor else statistic.backgroundColor,
                            icon = statistic.icon
                    )
                }
            }

            Text(
                fontSize = MaterialTheme.typography.headlineSmall.fontSize,
                fontWeight = MaterialTheme.typography.headlineSmall.fontWeight,
                fontStyle = MaterialTheme.typography.headlineSmall.fontStyle,
                color = Color.Gray,
                modifier = Modifier.padding(start = 16.dp, top = 8.dp),
                text = stringResource(id = R.string.upcomingToday)
            )


        }

    }


}